//
//  UIView+Addtion.m
//  CrossScreenVOD
//
//  Created by Dongfuming on 2017/12/21.
//  Copyright © 2017年 china-avs. All rights reserved.
//

#import "UIView+Addtion.h"
#import "AppDelegate.h"

@implementation UIView (Addtion)

- (void)makeToastWithRequestFailure:(NSError *)error
{
    switch (error.code) {
        case NSURLErrorTimedOut:
            [self showInfo:@"请求超时"];
            break;
        case NSURLErrorCannotConnectToHost:
            [self showInfo:@"服务器连接失败"];
            break;
        case NSURLErrorNotConnectedToInternet:
            [self showInfo:@"网络连接失败"];
            break;
        case 3840:
            [self showInfo:@"请求失败"];
            break;
        case NSURLErrorNetworkConnectionLost:
            [self showInfo:@"服务器断开"];
            break;
        case -1: // token无效
            [[AppDelegate sharedDelegate] doLogout];
            [kAppWindow showInfo:error.localizedDescription];
            break;
        default:
            [self showInfo:error.localizedDescription];
            break;
    }
}

- (void)showHUD
{
    [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kAppWindow animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [UIColor blackColor];
    hud.bezelView.layer.cornerRadius = 8;
    hud.margin = 15;
    hud.minSize = CGSizeMake(100, 100);
}

- (void)hideHUD
{
    [MBProgressHUD hideHUDForView:kAppWindow animated:YES];
}

- (void)showInfo:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kAppWindow animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.label.textColor = [UIColor whiteColor];
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = 15;
    hud.bezelView.layer.cornerRadius = 5;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    [hud hideAnimated:YES afterDelay:1.5];
}

- (void)showSuccess:(NSString *)message
{
    [self showHUDWithImageName:@"hud_success" message:message];
}

- (void)showError:(NSString *)message
{
    [self showHUDWithImageName:@"hud_error" message:message];
}

#pragma mark - Private
- (void)showHUDWithImageName:(NSString *)imageName message:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:kAppWindow animated:YES];
    hud.label.text = message;
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    hud.label.textColor = [UIColor whiteColor];
    hud.margin = 15;
    hud.bezelView.layer.cornerRadius = 5;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.minSize = CGSizeMake(100, 100);
    [hud hideAnimated:YES afterDelay:1.5];
}

@end
